<template>
	<view class="comment cu-card dynamic {{isCard?'no-card':''}}">
		<view class="cu-list menu-avatar comment solids-top">
			<view class="cu-item">
				<view class="cu-avatar round" :style="{ 'background-image': 'url(' + avatar + ')' }">
				</view>
				<view class="content">
					<view class="text-grey">{{name}}</view>
					<view class="text-gray text-content text-df">
						{{content}}
					</view>
					<view v-for="(item,index) in fa_commonentList" :key="index">
						<view class="bg-grey padding-sm radius margin-top-sm  text-sm">
							<view class="flex son_com">
								<view class="cu-avatar round" style="margin-right: 10px;"
									:style="{ 'background-image': 'url(' + item.avatar + ')' }">
								</view>
								<view>{{item.username}}:</view>
								<view class="flex-sub">{{item.content}}</view>
							</view>
						</view>
					</view>

					<view class="more" v-if="Son_more" @click="getmore">
						查看更多……
					</view>
					<view class="margin-top-sm flex justify-between">
						<view class="text-gray text-df">{{time.substring(0,10)}}</view>
						<!-- <text style="color: red;font-size: xx-small;" v-if="creatId==myid" @click="FaDelate">删除</text> -->
						<view @click="ShowInput">
							<!-- <text class="cuIcon-appreciatefill text-red"></text> -->
							<text class="cuIcon-messagefill text-gray margin-left-sm"></text>
						</view>
					</view>
				</view>
			</view>
		</view>



		<!-- 用于展示更多评论 -->
		<view class="cu-modal bottom-modal show" v-if="modalName">
			<view class="cu-dialog bg-white">
				<view class="cu-bar bg-white" style="justify-content: flex-end;">
					<!-- <view class="action text-green" @tap="hideModal">确定</view> -->
					<view class="action text-green" @tap="hideModal">确定</view>
				</view>
				<!-- <view class="padding-xl"> -->
				<view class="modal_content">
					<comment :content='content' :avatar='avatar' :name='name' :time='time' :id='id' :type='0'
						:createid='creatId'></comment>
				</view>
				<!-- </view> -->
				<!-- 				<view class="more" v-if="more" @click="getmore">
					查看更多……
				</view>
				<view class="more" v-else>
					没有更多了
				</view> -->
				<view class="cu-bar input" style="width: 100%;border: 0.5px solid 	#FFF8DC;" v-if="showInput">
					<input class="solid-bottom" :adjust-position="true" :focus="false" maxlength="300"
						cursor-spacing="10" style="text-align: start;" @focus="InputFocus" @blur="InputBlur"
						v-model="commentValue" />
					<button class="cu-btn bg-green shadow" @click="PostComment">发送</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getcommonents,postcomment,getSoncommonents } from '@/store/api.js'
	import comment from '@/subpkg/components/comment.vue'
	export default {
		components: {
			comment
		},
		// props: ['mesid', 'time', 'name', 'avatar', 'content', 'id', 'rootId', 'commentId'],
		props: ['avatar', 'content', 'time', 'name', 'id', 'creatId', 'mesid', 'time'], //id是根评论的id
		mounted() {
			const myid = uni.getStorageInfoSync('userid')
			this.$nextTick(() => {
				this.getFacomments(this.nowpage, 3)
			})
			uni.$on('CommentOne', value => {
				this.CommentOther.commentId = value.id
				this.CommentOther.userId = value.userid
				this.subType = 1
				this.showInput = true
				console.log(this.CommentOther)
			})
		},
		data() {
			return {
				sontotal: 0,
				nowpage: 1,
				Son_more: false,
				showInput: false,
				CommentValue: '',
				InputBottom: '',
				commentValue: '',
				more: true,
				modalName: false,
				fa_commonentList: [],
				subType: 0, //0是评论根，1是评论子
				CommentOther: {
					userId: '',
					commentId: ''
				}
			}
		},
		methods: {
			ShowInput() {
				this.subType = 0
				this.modalName = true
			},
			PostComment() {
				if (this.subType == 0) {
					const data = {
						postId: this.mesid,
						type: 0,
						rootId: this.id,
						toCommentId: this.id,
						toCommentUserId: this.creatId,
						content: this.commentValue
					}
					this.commentValue = ''
					this.showInput = false
					console.log(data)
					postcomment(data).then(res => {
						uni.$emit('sendFinish', this.id)
					})
				} else {
					const data = {
						postId: this.mesid,
						type: 0,
						rootId: this.id,
						toCommentId: this.CommentOther.commentId,
						toCommentUserId: this.CommentOther.userId,
						content: this.commentValue
					}
					this.commentValue = ''
					console.log(data)
					this.showInput = false
					postcomment(data).then(res => {
						uni.$emit('sendFinish', this.id)
					})
				}

			},
			getFacomments(Num, Size) {
				getSoncommonents(this.id, Num, Size).then(res => {
					console.log('这是获取子评论内容')
					console.log(res.data.data.total)
					// console.log(res)
					this.sontotal = res.data.data.total
					this.nowpage++
					res.data.data.rows.forEach((item, index) => {
						this.fa_commonentList.unshift({
							content: item.content,
							createBy: item.createBy,
							id: item.id,
							rootId: item.rootId,
							time: item.createTime,
							toCommentId: item.toCommentId,
							toCommentUserId: item.toCommentUserId,
							username: item.username,
							avatar: item.avatar
						})
					})
					if (this.fa_commonentList.length == total) {
						this.more = false
					}
				}).catch(err => {
					console.log(err)
				})
			},
			subComment() {

			},
			getsoncom(id) {

			},
			showModal(e) {
				this.modalName = true
			},
			hideModal(e) {
				this.modalName = false
				this.showInput = false
			},
			getcommonent() {
				setTimeout(() => {
					getcommonents(this.mesid, 1, 3).then(res => {
						console.log('这是获取所有评论')
						console.log(this.mesid)
						console.log(res)
					}).catch(err => {
						console.log(err)
					})
				}, 1000)

			}
		}
	}
</script>

<style scoped>
	.more {
		text-align: center;
	}

	.modal_content {
		max-height: 40vh;
		overflow-y: auto;
	}
</style>